{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4d7c3c07-fa91-4a64-8d49-701626e79455",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "Done processing all videos.\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "import os\n",
    "\n",
    "# 设置视频文件夹的路径\n",
    "video_folder_path = './'\n",
    "# 获取所有视频文件的路径\n",
    "video_files = [os.path.join(video_folder_path, f) for f in os.listdir(video_folder_path) if f.endswith(('.mp4', '.avi'))]\n",
    "print(video_files)\n",
    "\n",
    "for video_path in video_files:\n",
    "    # 读取视频文件\n",
    "    cap = cv2.VideoCapture(video_path)\n",
    "    if not cap.isOpened():\n",
    "        print(f\"Error opening video file {video_path}\")\n",
    "        continue\n",
    "\n",
    "    # 获取视频文件名（不包括扩展名）\n",
    "    video_name = os.path.splitext(os.path.basename(video_path))[0]\n",
    "    # 为该视频创建一个新的文件夹来保存帧，如果文件夹不存在\n",
    "    frame_save_path = os.path.join(video_folder_path, video_name)\n",
    "    if not os.path.exists(frame_save_path):\n",
    "        os.makedirs(frame_save_path)\n",
    "    \n",
    "    frame_count = 0\n",
    "    saved_frame_count = 0\n",
    "    while True:\n",
    "        # 读取帧\n",
    "        ret, frame = cap.read()\n",
    "        # 如果读取失败，说明视频结束了\n",
    "        if not ret:\n",
    "            break\n",
    "        \n",
    "        # 每隔6帧保存一帧\n",
    "        if frame_count % 6 == 0:\n",
    "            frame_filename = os.path.join(frame_save_path, f\"frame_{saved_frame_count}.jpg\")\n",
    "            cv2.imwrite(frame_filename, frame)\n",
    "            saved_frame_count += 1\n",
    "        frame_count += 1\n",
    "\n",
    "    cap.release()\n",
    "    print(f\"Finished processing {video_name}, saved {saved_frame_count} frames.\")\n",
    "\n",
    "print(\"Done processing all videos.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
